# @Author: dpf
# @Date  : 2024/12/03/16:03
import sys

from PyQt6 import QtCore, QtGui, QtWidgets
from PyQt6.QtCore import QModelIndex
from PyQt6.QtWidgets import QWidget, QApplication, QAbstractItemView, QSizePolicy, QHeaderView, QTableWidgetItem, \
    QMessageBox

from dao import bookTypeDao
from entity.BookTypeModel import BookType


class Ui_Form(QWidget):
    def __init__(self):
        super(Ui_Form, self).__init__()
        self.setWindowFlag(QtCore.Qt.WindowType.MSWindowsFixedSizeDialogHint)
        self.setupUi(self)
        self.initTable()

    def setupUi(self, Form):
        Form.setObjectName("Form")
        Form.resize(615, 626)
        self.groupBox = QtWidgets.QGroupBox(parent=Form)
        self.groupBox.setGeometry(QtCore.QRect(50, 40, 511, 91))
        self.groupBox.setObjectName("groupBox")
        self.label = QtWidgets.QLabel(parent=self.groupBox)
        self.label.setGeometry(QtCore.QRect(20, 40, 81, 16))
        self.label.setObjectName("label")
        self.s_bookTypeNameInput = QtWidgets.QLineEdit(parent=self.groupBox)
        self.s_bookTypeNameInput.setGeometry(QtCore.QRect(110, 40, 221, 21))
        self.s_bookTypeNameInput.setObjectName("s_bookTypeNameInput")
        self.search = QtWidgets.QPushButton(parent=self.groupBox)
        self.search.setGeometry(QtCore.QRect(350, 40, 75, 23))
        icon = QtGui.QIcon()
        icon.addPixmap(QtGui.QPixmap("../images/search.png"), QtGui.QIcon.Mode.Normal, QtGui.QIcon.State.Off)
        self.search.setIcon(icon)
        self.search.setObjectName("search")
        # 绑定搜索按钮的槽函数
        self.search.clicked.connect(self.initTable)
        self.bookTypeTable = QtWidgets.QTableWidget(parent=Form)
        self.bookTypeTable.setGeometry(QtCore.QRect(55, 160, 501, 192))
        self.bookTypeTable.setObjectName("bookTypeTable")
        self.bookTypeTable.setColumnCount(0)
        self.bookTypeTable.setRowCount(0)
        # 绑定表格行点击槽函数
        self.bookTypeTable.clicked.connect(self.initForm)

        self.groupBox_2 = QtWidgets.QGroupBox(parent=Form)
        self.groupBox_2.setGeometry(QtCore.QRect(40, 370, 511, 221))
        self.groupBox_2.setObjectName("groupBox_2")
        self.label_2 = QtWidgets.QLabel(parent=self.groupBox_2)
        self.label_2.setGeometry(QtCore.QRect(20, 30, 31, 16))
        self.label_2.setObjectName("label_2")
        self.label_3 = QtWidgets.QLabel(parent=self.groupBox_2)
        self.label_3.setGeometry(QtCore.QRect(180, 30, 81, 16))
        self.label_3.setObjectName("label_3")
        self.label_4 = QtWidgets.QLabel(parent=self.groupBox_2)
        self.label_4.setGeometry(QtCore.QRect(20, 70, 53, 15))
        self.label_4.setObjectName("label_4")
        self.idInput = QtWidgets.QLineEdit(parent=self.groupBox_2)
        self.idInput.setGeometry(QtCore.QRect(50, 30, 91, 21))
        self.idInput.setObjectName("idInput")
        self.idInput.setReadOnly(True)
        self.idInput.setStyleSheet("background-color: rgb(194, 194, 194);")

        self.bookTypeNameInput = QtWidgets.QLineEdit(parent=self.groupBox_2)
        self.bookTypeNameInput.setGeometry(QtCore.QRect(260, 30, 231, 21))
        self.bookTypeNameInput.setObjectName("bookTypeNameInput")
        self.bookTypeDescInput = QtWidgets.QPlainTextEdit(parent=self.groupBox_2)
        self.bookTypeDescInput.setGeometry(QtCore.QRect(50, 70, 441, 71))
        self.bookTypeDescInput.setObjectName("bookTypeDescInput")
        self.editBtn = QtWidgets.QPushButton(parent=self.groupBox_2)
        self.editBtn.setGeometry(QtCore.QRect(40, 170, 75, 23))
        icon1 = QtGui.QIcon()
        icon1.addPixmap(QtGui.QPixmap("../images/edit.png"), QtGui.QIcon.Mode.Normal, QtGui.QIcon.State.Off)
        self.editBtn.setIcon(icon1)
        self.editBtn.setObjectName("editBtn")
        # 绑定编辑按钮槽函数
        self.editBtn.clicked.connect(self.update)

        self.deleteBtn = QtWidgets.QPushButton(parent=self.groupBox_2)
        self.deleteBtn.setGeometry(QtCore.QRect(330, 170, 75, 23))
        font = QtGui.QFont()
        font.setPointSize(9)
        self.deleteBtn.setFont(font)
        icon2 = QtGui.QIcon()
        icon2.addPixmap(QtGui.QPixmap("../images/delete.png"), QtGui.QIcon.Mode.Normal, QtGui.QIcon.State.Off)
        self.deleteBtn.setIcon(icon2)
        self.deleteBtn.setIconSize(QtCore.QSize(14, 14))
        self.deleteBtn.setObjectName("deleteBtn")
        # 绑定删除按钮槽函数
        self.deleteBtn.clicked.connect(self.delete)
        self.retranslateUi(Form)
        QtCore.QMetaObject.connectSlotsByName(Form)

    def retranslateUi(self, Form):
        _translate = QtCore.QCoreApplication.translate
        Form.setWindowTitle(_translate("Form", "图书类别管理"))
        self.groupBox.setTitle(_translate("Form", "查询操作"))
        self.label.setText(_translate("Form", "图书类别名称"))
        self.search.setText(_translate("Form", "搜索"))
        self.groupBox_2.setTitle(_translate("Form", "表单操作"))
        self.label_2.setText(_translate("Form", "编号"))
        self.label_3.setText(_translate("Form", "图书类别名称"))
        self.label_4.setText(_translate("Form", "描述"))
        self.editBtn.setText(_translate("Form", "修改"))
        self.deleteBtn.setText(_translate("Form", "删除"))

    def initTable(self):
        s_bookTypeName = self.s_bookTypeNameInput.text()
        result = bookTypeDao.list(s_bookTypeName)
        row = 0
        if result:
            row = len(result)
        self.bookTypeTable.setColumnCount(3)
        self.bookTypeTable.setRowCount(row)
        self.bookTypeTable.verticalHeader().setVisible(False)  # 隐藏垂直标题 序号
        self.bookTypeTable.setEditTriggers(QAbstractItemView.EditTrigger.NoEditTriggers)  # 禁止编辑单元格
        self.bookTypeTable.setSizePolicy(QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Expanding)
        self.bookTypeTable.setHorizontalHeaderLabels(['编号', '图书类别名称', '图书类别描述'])
        self.bookTypeTable.horizontalHeader().setSectionResizeMode(QHeaderView.ResizeMode.Stretch)  # 列头自适应
        self.bookTypeTable.setSelectionBehavior(QAbstractItemView.SelectionBehavior.SelectRows)  # 设置选择行为，以行为单位

        for i in range(row):
            for j in range(3):
                data = QTableWidgetItem(str(result[i][j]))
                self.bookTypeTable.setItem(i, j, data)

    def initForm(self, index: QModelIndex):
        rowIndex = index.row()  # 获取行索引
        self.idInput.setText(self.bookTypeTable.item(rowIndex, 0).text())  # 设置编号
        self.bookTypeNameInput.setText(self.bookTypeTable.item(rowIndex, 1).text())  # 设置图书类别名称
        self.bookTypeDescInput.setPlainText(self.bookTypeTable.item(rowIndex, 2).text())  # 设置图书类别描述

    def update(self):
        id = self.idInput.text()
        if id.strip() == "":
            QMessageBox.information(None, '系统提示', '请选中您需要编辑的那行数据！')
            return

        bookTypeName = self.bookTypeNameInput.text()
        if bookTypeName.strip() == "":
            QMessageBox.information(None, '系统提示', '请输入图书类别名称！')
            return

        bookTypeDesc = self.bookTypeDescInput.toPlainText()
        bookType = BookType.my_constructor(id, bookTypeName, bookTypeDesc)
        if bookTypeDao.update(bookType) > 0:
            QMessageBox.information(None, '系统提示', '修改成功！')
            self.initTable()
        else:
            QMessageBox.warning(None, '系统提示', '修改失败！')

    def resetForm(self):
        self.idInput.setText("")
        self.bookTypeNameInput.setText("")
        self.bookTypeDescInput.setPlainText("")

    def delete(self):
        id = self.idInput.text()
        if id.strip() == "":
            QMessageBox.information(None, '系统提示', '请选中您需要编辑的那行数据！')
            return
        reply = QMessageBox.question(None, '系统提示', '确定删除',
                                     QMessageBox.StandardButton.Yes | QMessageBox.StandardButton.No,
                                     QMessageBox.StandardButton.No)
        if reply == QMessageBox.StandardButton.Yes:
            if bookTypeDao.delete(id) > 0:
                QMessageBox.information(None, '系统提示', '删除成功！')
                self.initTable()
                self.resetForm()
            else:
                QMessageBox.warning(None, '系统提示', '删除失败！')


if __name__ == '__main__':
    app = QApplication(sys.argv)
    ui = Ui_Form()
    ui.show()
    sys.exit(app.exec())
